//
//  WelcomeView.swift
//  Swift微博
//
//  Created by 陈竹 on 16/2/26.
//  Copyright © 2016年 陈竹. All rights reserved.
//

import UIKit

class WelcomeView: UIView {
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        self.backgroundColor=UIColor.redColor()
       setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 

    override func didMoveToWindow() {
        super.didMoveToWindow()
        //更新约束
        avatarView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-100)
        }
        UIView.animateWithDuration(
            1.2,
            delay: 0.0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                //调用这个方法会更新约束
               self.layoutIfNeeded()
            }) { (_) -> Void in
                self.removeFromSuperview()
        }
        
    }
    
    
    
    private lazy var avatarView=UIImageView(cz_imageName: "avatar_default_big")
    private lazy var welcomelabel=UILabel(text: "欢迎归来", fontSize: 18)
    
}

extension WelcomeView{
    
    func setupUI(){
        //设置背景
        backgroundColor=UIColor(patternImage: UIImage(named: "ad_background")!)
        
        
        //添加控件
        addSubview(avatarView)
        addSubview(welcomelabel)
        
        //设置头像的宽高
        let iconWH:CGFloat=45
        avatarView.layer.cornerRadius=iconWH
        avatarView.layer.masksToBounds=true
        
        //自动布局
        avatarView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(100)
            make.size.equalTo(CGSize(width: iconWH*2, height: iconWH*2))
        }
        welcomelabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(avatarView)
            make.top.equalTo(avatarView.snp_bottom).offset(20)
        }
        //强行更新约束
        layoutIfNeeded()
        
        avatarView.cz_setImageWithURL(UserAccount.sharedUserAccount.avatar_large, placeholderName: "avatar_default_big")
        
    }
    
}